<?php
class GroupsController extends AppController {

	var $name = 'Groups';
	var $uses = array('Group');
	
	var $paginate = array(
		'Group' => array(
	        'limit' => 10,
	        'order' => array(
	            'Group.id' => 'DESC'
	        )
		)
    );
    
	/**
	 * Group Model
	 *
	 * @var Group
	 */
	var $Group;
	
	/**
	 * Tyk zadavame spisaka na action-ite s publichen dostyp (action-ite bez authorizaciq)
	 */
	function __initPublicActions() {
		$this->Auth->allow(array(
			'init_db'
		));
	}
	
	function beforeFilter() {
        parent::beforeFilter();
        
        $this->__initPublicActions();
    }
	
    function init_db() {
    	$this->loadModel('Group');
    	
    	$group =& $this->Group;
    	
    	//------------- root [id=1]
    	$group->id = 1;
    	
    	$this->Acl->allow($group, 'controllers');
    	//------------/ root [id=1]
    	
    	//------------- user [id=2]
    	$group->id = 2;
    	
    	
    	//------------/ user [id=2]
    	
    	echo "all done";
    	exit;
    }
    
	function admin_index() {
		$this->Group->recursive = 0;
		$this->set('groups', $this->paginate());
	}
	
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Невалиден елемент');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
		
		$group = $this->Group->read(null, $id);
		$parentGroup = $this->Group->findById($group['Group']['parent_id']); 
		
		$this->set('group', $group);
		$this->set('parentGroup', $parentGroup);
	}
	
	function admin_add() {
		$this->Group->setValidation('admin_add');
		$fields_to_save = array('title', 'parent_id');
		
		if (!empty($this->data)) {
			if(empty($this->data['Group']['parent_id'])) {
				$this->data['Group']['parent_id'] = 0;
			}
			
			$this->Group->create();
			if ($this->Group->save($this->data, true, $fields_to_save)) {
				$this->Session->setFlash('Елемента е записан успешно');
				$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
			} else {
				$this->Session->setFlash('Елемента не може да се запише. Моля, опитайте отново.');
			}
		}
		
		$groups = $this->Group->find('list');
		$this->set('parents', $groups);
	}
	
	function admin_edit($id = null) {
		$this->Group->setValidation('admin_edit');
		$fields_to_save = array('title', 'parent_id');
		
		if (!$id && empty($this->data)) {
			$this->Session->setFlash('Невалиден елемент');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
		/**
		 * Ako e ot zapazenite grupi, togava ne mogat da se redaktirat
		 */
		if (($id==1) || ($id==2)) {
			$this->Session->setFlash('Невалиден елемент');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
		if (!empty($this->data)) {
			if(empty($this->data['Group']['parent_id'])) {
				$this->data['Group']['parent_id'] = 0;
			}
			
			if ($this->Group->save($this->data, true, $fields_to_save)) {
				$this->Session->setFlash('Елемента е записан успешно');
				$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
			} else {
				$this->Session->setFlash('Елемента не може да се запише. Моля, опитайте отново.');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Group->read(null, $id);
		}
		
        $groups = $this->Group->find('list');
        $this->set('parents', $groups);
	}
	
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Невалиден елемент');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
		/**
		 * Ako e ot zapazenite grupi, togava ne mogat da se iztrivat
		 */
		if (($id==1) || ($id==2)) {
			$this->Session->setFlash('Невалиден елемент');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
		if ($this->Group->delete($id)) {
			$this->Session->setFlash('Елемента е изтрит успешно');
			$this->redirect(array('admin' => true, 'plugin' => null, 'controller' => 'groups', 'action' => 'index'));
		}
	}
	
}
?>